﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TPAMain工序结余锁定工具
{
    public partial class frmUnlock : Form
    {
        string strOperator = string.Empty;
        DataTable dt;       //数据集

        /// <param name="iTmpLockMode">锁定模式：1、指定工单；2、指定批号；3、指定芯片</param>
        /// <param name="strTmpKey"></param>
        /// <param name="tmpdt"></param>
        public frmUnlock(string strTmpOperator,DataTable tmpdt)
        {
            InitializeComponent();
            strOperator = strTmpOperator;
            this.Text = "解锁记录";
            dt = tmpdt;
            dgv1.DataSource = dt;
            dgv1.AutoResizeColumns();
        }

        private void btnOperate_Click(object sender, EventArgs e)
        {
            if (dgv1.Rows.Count == 0 )
            {
                GeneralTools.ShowInfoMsg("没有要处理的数据！");
                return;
            }
            if (txtReason.Text.Trim().Equals(string.Empty))
            {
                GeneralTools.ShowInfoMsg("解锁原因不能为空！");
                return;
            }
            string strUnlockReason = txtReason.Text.Trim();
            DialogResult dr = MessageBox.Show("确认解锁选定的记录吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.OK)
            {
                //创建锁定清单
                DataTable tmpdt = new DataTable();
                tmpdt.Columns.Add("解锁明细ID");
                tmpdt.Columns.Add("工序结余ID");
                tmpdt.Columns.Add("原工序结余");
                tmpdt.Columns.Add("原备注");
                for (int i=0;i<dgv1 .Rows.Count;i++)
                {
                    if (chkUnlockSelected.Checked && dgv1.Rows[i].Selected == false )
                    {
                        continue;
                    }
                    DataRow tmpdr = tmpdt.NewRow();
                    tmpdr["解锁明细ID"] = dgv1.Rows[i].Cells["序号"].Value;
                    tmpdr["工序结余ID"] = dgv1.Rows[i].Cells["工序结余ID"].Value;
                    tmpdr["原工序结余"] = dgv1.Rows[i].Cells["原结余数量"].Value;
                    tmpdr["原备注"] = dgv1.Rows[i].Cells["原备注"].Value;
                    tmpdt.Rows.Add(tmpdr);
                }
                if (LockHelper.DoUnlock(strOperator, strUnlockReason, tmpdt))
                {
                    if (chkUnlockSelected.Checked)
                    {
                        GeneralTools.ShowInfoMsg(dgv1.SelectedRows .Count.ToString() + "条记录解锁成功！");
                    }
                    else
                    {
                        GeneralTools.ShowInfoMsg(dgv1.Rows.Count.ToString() + "条记录解锁成功！");
                    }
                    this.Close();
                }
            }
        }
    }
}
